package com.yxzx.statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxzx.statistics.client.UcenterClient;
import com.yxzx.statistics.entity.StatisticsDaily;
import com.yxzx.statistics.mapper.StatisticsDailyMapper;
import com.yxzx.statistics.service.StatisticsDailyService;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author kongfuzi
 * @since 2020-08-15
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;
    @Override
    public int createStatisticsByDay(String day) {
        //删除已存在的统计对象
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("date_calculated",day);
        baseMapper.delete(queryWrapper);

        //获取统计信息
        Integer registerNum = (Integer) ucenterClient.countRegister(day).getData().get("item");//获取每日注册人数
        Integer loginNum = RandomUtils.nextInt(100,200);//模拟每日登录人数
        Integer videoViewNum = RandomUtils.nextInt(100,200);//模拟每日播放人数
        Integer courseNum = RandomUtils.nextInt(100,200);//模拟每日更新课程数
        //创建统计对象
        StatisticsDaily daily = new StatisticsDaily();
        daily.setRegisterNum(registerNum);
        daily.setLoginNum(loginNum);
        daily.setVideoViewNum(videoViewNum);
        daily.setCourseNum(courseNum);
        daily.setDateCalculated(day);
        //添加数据
        return baseMapper.insert(daily) ;
    }

    @Override
    public Map<String, Object> getChartData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(type,"date_calculated");
        queryWrapper.between("date_calculated",begin,end);
        queryWrapper.orderByAsc("date_calculated");
        List<StatisticsDaily> dailyList = baseMapper.selectList(queryWrapper);


        Map<String,Object> map = new HashMap<>();//创建Map对象存储数据
        List<Integer> dataList = new ArrayList<>();//创建dataList存储统计数据
        List<String>  dateList = new ArrayList<>();//创建dataList存储统计日期
        map.put("dataList",dataList);
        map.put("dateList",dateList);

        for (int i = 0, j = dailyList.size(); i < j; i++) {
            StatisticsDaily daily = dailyList.get(i);
            dateList.add(daily.getDateCalculated());

            switch (type){
                case "register_num":
                    dataList.add(daily.getRegisterNum());
                    break;
                case "login_num":
                    dataList.add(daily.getLoginNum());
                    break;
                case "video_view_num":
                    dataList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    dataList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        return map;
    }
}
